package leetcode.lcp.lcp20220924;

import leetcode.helper.tree.TreeNode;

//Solution4Test
public class Solution4 {

	class Info {
		int allClose;// close all
		int allOpen;// open all
		int rootOpen;//
		int rootClose;
	}

	public int closeLampInTree(TreeNode root) {
		Info ans = this.f(root);
		return ans.allClose;
	}

	private Info f(TreeNode root) {
		if (root == null) {
			return new Info();
		}
		Info ans = new Info();
		Info left = this.f(root.left);
		Info right = this.f(root.right);
		ans.allClose = this.closeAll(root.val, left, right);
		ans.allOpen = this.openAll(root.val, left, right);
		ans.rootOpen = this.rootOpen(root.val, left, right);
		ans.rootClose = this.rootClose(root.val, left, right);
		return ans;
	}

	// x VVVV
	private int rootClose(int val, Info left, Info right) {
		if (val == 1) {// 当前是开启v
			int ans1 = left.allOpen + right.allOpen + 1;//
			int ans2 = left.allClose + right.allClose + 1;//
			int ans3 = left.rootOpen + right.rootOpen + 3;
			int ans4 = left.rootClose + right.rootClose + 1;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		} else {// 当前是关闭
			int ans1 = left.allOpen + right.allOpen;//
			int ans2 = left.allClose + right.allClose + 2;//
			int ans3 = left.rootOpen + right.rootOpen + 2;
			int ans4 = left.rootClose + right.rootClose + 2;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		}
	}

	// v xxxx
	private int rootOpen(int status, Info left, Info right) {
		if (status == 1) {// 当前是开启
			int ans1 = left.allOpen + right.allOpen + 2;//
			int ans2 = left.allClose + right.allClose;//
			int ans3 = left.rootOpen + right.rootOpen + 2;
			int ans4 = left.rootClose + right.rootClose + 2;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		} else {// 当前是关闭x
			int ans1 = left.allOpen + right.allOpen + 1;//
			int ans2 = left.allClose + right.allClose + 1;//
			int ans3 = left.rootOpen + right.rootOpen + 1;
			int ans4 = left.rootClose + right.rootClose + 3;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		}
	}

	// v vvvv
	private int openAll(int status, Info left, Info right) {
		if (status == 1) {// 当前是开启
			int ans1 = left.allOpen + right.allOpen;//
			int ans2 = left.allClose + right.allClose + 2;//
			int ans3 = left.rootOpen + right.rootOpen + 2;
			int ans4 = left.rootClose + right.rootClose + 2;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		} else {// 当前是关闭x
			int ans1 = left.allOpen + right.allOpen + 1;//
			int ans2 = left.allClose + right.allClose + 1;//
			int ans3 = left.rootOpen + right.rootOpen + 3;
			int ans4 = left.rootClose + right.rootClose + 1;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		}
	}

	// x xxxxx
	private int closeAll(int status, Info left, Info right) {
		if (status == 1) {// 当前是开启 v
			int ans1 = left.allOpen + right.allOpen + 1;
			int ans2 = left.allClose + right.allClose + 1;
			int ans3 = left.rootOpen + right.rootOpen + 1;
			int ans4 = left.rootClose + right.rootClose + 3;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		} else {// 当前是关闭 x
			int ans1 = left.allOpen + right.allOpen + 2;
			int ans2 = left.allClose + right.allClose;
			int ans3 = left.rootOpen + right.rootOpen + 2;
			int ans4 = left.rootClose + right.rootClose + 2;
			return Math.min(Math.min(ans4, ans3), Math.min(ans1, ans2));
		}
	}
}
